public class paginationpagecontroller {

    //Instantiate the StandardSetController  
    public ApexPages.StandardSetController con{get; set;} 
    public List<Account> con1{get; set;} 
    
    private Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public Map<String, Schema.SObjectField> field_map{get;set;}
    public List<SelectOption> objectnames{get;set;}
    public List<SelectOption> fieldnames{get;set;}
    public List<String> objectnamesmapketset{get;set;}
    public Set<String> Field_Name {get;set;}
    public String selectedObject {get;set;}
    public String selectedfield {get;set;}
    public Boolean fieldpicklist{get;set;}
    public Boolean objectselected{get;set;}
    public String queryString {get;set;}
    public List<Sobject> allobjectrecords{get;set;}
    public List<fieldvalues> fvaluelist{get;set;}
    
    
  
    public List<SelectOption> getAllObjectNames(){
           
              objectnames= new List<SelectOption>();
              objectnamesmapketset= new List<String>(schemaMap.keySet());
              objectnamesmapketset.sort();
              objectnames.add(new SelectOption('None','--Select Object--'));
               for(String name :objectnamesmapketset)               
                   {
                       objectnames.add(new SelectOption(name,name));
                   }                
                        return objectnames;                
                }
                
          public List<SelectOption> FieldDescribe()
            {
                fieldnames=new List<SelectOption>();
                fieldnames.add(new SelectOption('None','--Select Field--'));
              try{
                  
                   if(selectedObject!=null)
                       {  
                         field_map=schemaMap.get(selectedObject).getDescribe().fields.getMap();                        
                        //Field_Name=field_map.keyset();
                       for(Schema.SObjectField fil: field_map.values())
                         {
                          schema.describefieldresult results=fil.getDescribe();
                           fieldnames.add(new SelectOption(results.getName(),results.getName()));
                         
                         }
                       }
                 
                  }catch(Exception e){
                  
                  }
            
              return fieldnames;
            
            }
        public List<Sobject> Allrecords()
         {
         allobjectrecords=new List<Sobject>();
            try{
                
                if(con != null){
              
                   allobjectrecords.addAll((List<Sobject>)con.getRecords());
                    
                 }
              }catch(Exception e){
            
            }
            return allobjectrecords;
         }
       
   public paginationpagecontroller()  
     {  
           
         fieldpicklist=false;
         objectselected=false;
     }  
   public void Search()  
    {       
    if(selectedObject!=null && selectedObject!='None')
      {        
        queryString='Select Id from '+selectedObject; 
                    
            con= new ApexPages.StandardSetController(Database.getQueryLocator(queryString));     
            //sets the number of records in each page set  
            con.setPageSize(5); 
            fieldpicklist=true; 
            objectselected=true;
            
            Allrecords();
            FieldDescribe();
      }
    
    }
    
    
    
    public void Addcolumn()
      {
          queryString=null;
          con=null;
          fvaluelist=new List<fieldvalues>();
     
             if(selectedfield!=null && selectedfield!='None')
               {
                   queryString='Select Id,'+selectedfield+' '+'from '+selectedObject;
                   con= new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                   con.setPageSize(5); 
               }
               
             for(Sobject  sobj: con.getRecords())
               {

                 fieldvalues fvalues=new fieldvalues();
                 fvalues.name=(String)sobj.get(selectedfield);
                 fvaluelist.add(fvalues);
              } 
        
       
      
      
      }
      
     public class fieldvalues{
                                   
      public String name{get;set;}
      public String Id{get;set;}
                              
                }
   //Boolean to check if there are more records after the present displaying records  
    public Boolean hasNext {  
    get  
        {  
            return con.getHasNext();  
        }  
        set;  
     }
   //Boolean to check if there are more records before the present displaying records  
    public Boolean hasPrevious  
    {  
        get  
        {  
            return con.getHasPrevious();  
        }  
        set;  
    }
  //Page number of the current displaying records  
    public Integer pageNumber  
    {  
        get  
        {  
            return con.getPageNumber();  
        }  
        set;  
    }
    //Returns the previous page of records  
    public void previous()  
    {  
        con.previous();  
    }  
   
   //Returns the next page of records  
    public void next()  
    {  
        con.next();  
    } 
}